package YZ.Students.Mier.controller.web;

import YZ.Students.Mier.Dto.EmailDto;
import YZ.Students.Mier.Service.UserService;
import YZ.Students.Mier.Service.impl.UserServiceImpl;
import YZ.Students.Mier.Tool.utils.BaseConstants;
import YZ.Students.Mier.Tool.utils.EmiailUtils;
import YZ.Students.Mier.controller.BaseServlet;
import YZ.Students.Mier.entity.UserEntity;
import com.alibaba.fastjson.JSON;
import javax.servlet.annotation.WebServlet;
import java.util.Date;
import java.util.Map;

@WebServlet("/UserServlet/*")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    public String register() throws Exception {
        String s = req.getReader().readLine();
        UserEntity userEntity = JSON.parseObject(s, UserEntity.class);
        System.out.println("userEntity"+userEntity);
        userService.register(userEntity);
        String activationCode = userEntity.getActivationCode();
        EmailDto emailDto = new EmailDto();
        emailDto.setTilte("Blog新用户注册账号激活");
        emailDto.setToUser(userEntity.getEmail());
        emailDto.setContent("<a href = 'http://127.0.0.1:8080/UserServlet/activation?code=" + activationCode + "'>点击这里激活</a>");
        EmiailUtils.sendEmailInfoUser(emailDto);
        return outJson(true);
    }
    //激活
    public String activation() throws Exception {
        // 1.获取激活码
        String activationCode = req.getParameter("code");
        System.out.println("激活码:" + activationCode);
        UserEntity userEntity = userService.byActivationCode(activationCode);
        if (userEntity == null) {
            System.out.println("激活码有误");
            return BaseConstants.BASE_SCRIPT + ":激活码有误";
        } else {
            userEntity.setStatus(2);
            userService.update(userEntity);
            return BaseConstants.BASE_REDIRECT + ":PageServlet/view/site/login.html";
        }
    }

    public String login() throws Exception {
      String s =  req.getReader().readLine();
        UserEntity userEntity = new UserEntity();
        userEntity = JSON.parseObject(s,UserEntity.class);
        userEntity = userService.login(userEntity.getUsername(),userEntity.getPassword());
        if (userEntity ==null){
            return outJson(100,"用户名或者密码错误");
        }
        userEntity.setPassword(null);
        req.getSession().setAttribute("loginUser",userEntity);
        return outJson(200,"登录成功");
    }
    public String changePassword() throws Exception{
        String s = req.getReader().readLine();
        Map mapp = JSON.parseObject(s);
        UserEntity userEntity = (UserEntity) req.getSession().getAttribute("loginUser");
        Integer id = userEntity.getId();
        System.out.println("s:"+s);
        System.out.println("id;"+userEntity);
        System.out.println("userEntity:"+userEntity.getId());
        boolean result = userService.changePassword(mapp,id);
        if (!result){
            return outJson(100,"修改失败");
        }
        return outJson(200,"修改成功");
    }
}
